// pages/coupon/index.js

import {
    getNavigationbar
} from "../../api/index.js"
Page({

    /**
     * 页面的初始数据
     */
    data: {show:false,
        formData: [],
        scrollViewHeight: 0,
        width: 0,addressId:'',
        stickyTop: 0,
        lin:0,
        heishouww:false,
        cartTotalPrice:0,discount:0
    },
    /**
     * 计算页面可滚动区域的动态高度
     */
    computeScrollViewHeight() {
        let that = this
        wx.getSystemInfo({
            success: function (res) {
                const windowHeight = res.windowHeight + 70;
                console.log('手机屏幕高度：', windowHeight);

                // 进行适配操作，例如设置元素高度为屏幕高度的一半
                const halfScreenHeight = windowHeight / 2;
                console.log('屏幕高度的一半：', halfScreenHeight);
                that.setData({
                    scrollViewHeight: windowHeight
                })
            }
        });
    },
    handleScrollToLower() {
        console.log('触发到底部提示打印');
        if(this.data.lin == 1) {
            this.setData({
                currentPage: this.data.currentPage + 1,
            });
            this.couponRegion();
        }
      
    },
    couponRegion(){
        getNavigationbar({
            act: 'coupon_region',
            p: this.data.goodListP
        }).then(res => {
            res.map(item => {
                item.starttimeStr = this.formatDate(item.starttime)
                item.exptimeStr = this.formatDate(item.exptime)
                if (item.exptime > Date.parse(new Date()) / 1000) {
                    item.overdue = true;
                }
                if(item.discounttype == '2') {
                    item.edu = item.edu / 10
                } else if (item.discounttype == '3') {
                    item.edu = item.edu / 10
                }
            })
            console.log('res',res)
            this.setData({
                formData: res,
                show:true,
                heishouww:false
            });
        })
    },
    getData() {
        getNavigationbar({
            act: 'coupon_list',
        }).then(res => {
            res.map(item => {
                item.starttimeStr = this.formatDate(item.starttime)
                item.exptimeStr = this.formatDate(item.exptime)
                if (item.exptime > Date.parse(new Date()) / 1000) {
                    item.overdue = true;
                }
                if(item.discounttype == '2') {
                    item.edu = item.edu / 10
                } else if (item.discounttype == '3') {
                    item.edu = item.edu / 10
                }
            })
            console.log('res',res)
            this.setData({
                formData: res,
                show:true,
                heishouww:false
            });
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
  
        this.data.type = options.type
        this.data.addressId = options.arr
        this.data.discount = options.discount
        this.data.lin = options.lin
        console.log('options',options,this.data.lin)
        if(options.lin != undefined) {
            this.couponRegion() 
            this.setData({
                lin:this.data.lin
            })
        } else {
            if(options.data) {
                this.data.cartTotalPrice = options.cartTotalPrice
                console.log('测试',options)
               let list = JSON.parse(options.data)
               list.map(item => {
                    console.log('this.formatDate(item.starttime)',this.formatDate(item.starttime))
                    item.starttimeStr = this.formatDate(item.starttime)
                    item.exptimeStr = this.formatDate(item.exptime)
                    if (item.exptime > Date.parse(new Date()) / 1000) {
                        item.overdue = true;
                    }
                    if(item.discounttype == '2') {
                        item.edus = item.edu / 10
                    } else if (item.discounttype == '3') {
                        item.edus = item.edu / 10
                    } else if (item.discounttype == '1') {
                        item.edus = item.edu
                    }
    
                })
                console.log('JSON.parse(options.data)',list)
                this.setData({
                    formData:list,
                    show:true,
                    heishouww:true,
                });
            } else {
               this.getData()
            }
        }
        if(options.title) {
            wx.setNavigationBarTitle({
                title: options.title // 设置标题名称为"商品详情"
            });
        }
      
        this.computeScrollViewHeight()

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
       

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },

    formatDate(timestamp) {
        // 将时间戳转换为Date对象
        var date = new Date(timestamp * 1000);
        // 获取年月日时分秒
        var year = date.getFullYear();
        var month = date.getMonth() + 1; // 月份是从0开始的
        var day = date.getDate();
        // 为单个数字前面补零
        month = month < 10 ? '0' + month : month;
        day = day < 10 ? '0' + day : day;
        // 组合成日期格式
        return year + '-' + month + '-' + day;
    },

    gotoDetail(e) {
        console.log('e',e)
        let value = e.currentTarget.dataset.value
        value.hanType = 'fromtype'
        value.addressId =  this.data.addressId
        value.cartTotalPrice =  this.data.cartTotalPrice
        value.discount = this.data.discount
       
        if (this.data.type != 'orderForm') {
            wx.navigateTo({
                url: `/pages/coupon_detail/index?data=${JSON.stringify(value)}`,
            })
        } else {
            // 返回上一个页面并携带参数
            wx.redirectTo({
                url: `/subpackages/pages/confirm_order_shipping/index?data=${JSON.stringify(value)}`,
            })
        }
    },

    exchange() { let that = this
        wx.showModal({
            editable: true, //显示输入框
            title: '优惠券编码', //提示的标题
            placeholderText: '请输入优惠券编码',
            success: function (res) {
               
               
                if (res.confirm) {
                    getNavigationbar({
                        act: 'getcouponbycode',
                        code: res.content
                    }).then(response => {
                        console.log('Dunhuang',response)
                        if(response.result == 101) {
                            wx.nextTick(() => {
                                getApp().Tips({
                                    title:response.msg
                                })
                               
                            })
                        } else {
                            wx.nextTick(() => {
                                that.getData()
                               
                            })
                            getApp().Tips({
                                title:'兑换成功'
                            })
                        }
                       
                        
                    })
                } else if (res.cancel) {
                    console.log('用户点击了取消')
                }
            }
        })
    }

})